using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Common;
using umbraco.presentation.nodeFactory;
using umbraco.cms.businesslogic.media;

namespace CommerceForUmbraco.usercontrols
{
    public partial class catalog : System.Web.UI.UserControl
    {
        #region Properties

        protected string thisLink = "";
        protected string categoryName = "";
        protected int categoryID = 0;
        protected string categoryGUID = string.Empty;
        DataSet ds = null;

        private string _PropCategory;

        public string PropCategory
        {
            get { return _PropCategory; }
            set { _PropCategory = value; }
        }

        private bool _DontRedirectToCart;

        public bool DontRedirectToCart
        {
            get
            {
                return _DontRedirectToCart;
            }
            set
            {
                _DontRedirectToCart = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Utility.IsGUID(PropCategory))
                categoryGUID = PropCategory;

            if (int.TryParse(PropCategory, out categoryID) == false)
                categoryID = 0;

            if (PropCategory != string.Empty && categoryID == 0)
                categoryName = PropCategory;

            if (!Page.IsPostBack)
            {

                if (PropCategory != string.Empty)
                {
                    LoadData();
                    LoadPage();
                }
            }
        }

        void LoadData()
        {
            //this page will bounce if the dataset didn't fire properly

            if (categoryName != string.Empty)
            {
                ds = CategoryController.GetPageByName(categoryName);
                thisLink = Page.ResolveUrl("~/catalog/" + categoryName + ".aspx");
            }
            else if (categoryID != 0)
            {
                ds = CategoryController.GetPageByID(categoryID);

            }
            else if (categoryGUID != string.Empty)
            {
                ds = CategoryController.GetPageByGUID(categoryGUID);
                thisLink = Page.ResolveUrl("~/catalog/" + categoryGUID + ".aspx");
            }

        }

        void LoadPage()
        {
            if (ds != null)
            {

                categoryID = (int)ds.Tables[0].Rows[0]["categoryID"];
                categoryName = ds.Tables[0].Rows[0]["categoryName"].ToString();

                categoryDisplayName.Text = categoryName;
                
                BindProductList();
                
            }

        }
        
        void BindProductList()
        {
            try
            {
                dtProducts.DataSource = ds.Tables[4];

                foreach (DataRow dr in ds.Tables[4].Rows)
                {
                    int mediaId = int.Parse(dr["imageFile"].ToString());
                    dr["imageFile"] = new Media(mediaId).getProperty("umbracoFile").Value.ToString();
                }

                dtProducts.DataBind();
            }
            catch
            {
                // set safe display
            }

        }

        protected void addToCart_Command(Object sender, CommandEventArgs e)
        {
            Guid productGUID = new Guid(e.CommandArgument.ToString());

            Commerce.Common.Product prod = new Commerce.Common.Product("productGUID", productGUID);
            prod.Quantity = 1;
            OrderController.AddItem(prod);

            if (DontRedirectToCart)
            {
                CartEvent.Add(prod.ProductID, 1);

            }
            else
            {
                // send to cart page
                Response.Redirect(Utility.GetCartUrl(), true);
            }
        }

    }
}
